package com.yh.correct.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.correct.dao.ISysPermissionMapper;
import com.yh.correct.dao.ISysRolePermissionMapper;
import com.yh.correct.model.SysPermission;
import com.yh.correct.model.vo.Tree;
import com.yh.correct.service.ISysPermissionService;

/**
 * 权限管理service层接口实现类
 * @author Administrator
 *
 */
@Service
public class SysPermissionServiceImpl implements ISysPermissionService {

	/**
	 * 使用logger日志
	 */
    private static Logger logger = LogManager.getLogger(SysPermissionServiceImpl.class);
    
    @Autowired
    private ISysPermissionMapper permissionMapper;
    
    @Autowired
    private ISysRolePermissionMapper rolePermissionMapper;
    /**
	 * 获取单个权限。
	 */
	@Override
	public SysPermission getSysPermission(SysPermission permission) throws Exception {
		// TODO Auto-generated method stub
		return permissionMapper.getSysPermission(permission);
	}
    /**
	 * 增加权限
	 */
	@Override
	public int insertSysPermission(SysPermission permission) throws Exception {
		// TODO Auto-generated method stub
		return permissionMapper.insertSysPermission(permission);
	}
    /**
	 * 删除权限。
	 * 1.权限表。2.角色权限关系表
	 */
	@Override
	@Transactional
	public int deleteSysPermission(SysPermission permission) throws Exception {
		// TODO Auto-generated method stub
		int i=permissionMapper.deleteSysPermission(permission);
		i+=rolePermissionMapper.deleteByPermissionId(permission.getId());
		return i;
	}
    /**
	 * 更新权限
	 */
	@Override
	public int updateSysPermission(SysPermission permission) throws Exception {
		// TODO Auto-generated method stub
		return permissionMapper.updateSysPermission(permission);
	}
    /**
	 * 动态条件查询权限分页
	 */
	@Override
	public PageInfo<Object> findListByCondition(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		PageHelper.startPage((int)condition.get("pageNum"), (int)condition.get("pageSize"), true);
		PageInfo<Object> pageInfo=new PageInfo(permissionMapper.findListByCondition(condition));
		return pageInfo;
	}
	/**
	 *查询权限Tree结构
	 */
	@Override
	public List<Tree> findTreeList() throws Exception {
		// TODO Auto-generated method stub
		return permissionMapper.findTreeList();
	}
	/**
	 *查询所有的权限
	 */
	@Override
	public List<SysPermission> findPermissionAll() throws Exception {
		// TODO Auto-generated method stub
		return permissionMapper.findPermissionAll();
	}

}
